#!/usr/bin/python

'''
watchlist.py
Copyright (C) 2011 Pradeep Balan Pillai

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>
'''


import gtk
import pickle


class Watchlist:
    def __init__(self):
        self.tickers = []

	# Load the tickers from pickled list of stocks 
    def load_tickers(self):
        ticker_file = open('ticker_list', 'rb')
        if(ticker_file.readlines() == []):  # Check whether the file contains data
            ticker_file.close()             # else add data before initiating
            self.add_stock()
            ticker_file = open('ticker_list','rb')
            self.tickers = pickle.load(ticker_file)
        else:
            ticker_file = open('ticker_list','rb')
            self.tickers = pickle.load(ticker_file)
        ticker_file.close()

    
    # Add stocks to watchlist. Argument should be a list of stock codes
    def add_stocks(self, stocks = []):
        pickled_watchlist = []
        pickled_stocklist = {}
        f = open('watch_list', 'rb')    # Load existing watchlist add new stocks and write 
        f1 = open('stock_list', 'rb')
        try:
            pickled_watchlist = pickle.load(f)
            f.close()
        except EOFError:
            pickled_list = []           # In case the watch list file is empty
        try:
            pickled_stocklist = pickle.load(f1)
            f1.close()
            for item in stocks:
                pickled_watchlist.append(pickled_stocklist[item])
            f = open('watch_list', 'wb')
            pickle.dump(pickled_watchlist, f)
            f.close()
        except EOFError:
            print "ERROR:File error"
            f.close()
            return 0 


    # Return the current watchlist as a list of (stock name, stock code) pairs
    def get_watchlist(self):
        watchlist = []
        pickled_watchlist = []
        pickled_stocklist = {}
        f = open('watch_list', 'rb')
        f1 = open('stock_list', 'rb')
        try:
            pickled_watchlist = pickle.load(f)
            f.close()
            pickled_stocklist = pickle.load(f1)
            f1.close()
            stocklist = pickled_stocklist.items()
            for stockcode in pickled_watchlist:
                for item in stocklist:
                    if stockcode == item[1]:
                        watchlist.append((item[0], stockcode))
            return watchlist
        except EOFError:
            print 'ERROR:File error'
            f.close()
            f1.close()
            return 0
    def delete_stocks(self, stocks = {}):
        pass
